﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Threading;

namespace FieldTools.Util
{
    public static partial class Extention
    {
        /// <summary>
        /// 字典类型转化为对象,大驼峰
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static T DicToObject<T>(this Dictionary<string, object> dic) where T : new()
        {
            var md = new T();
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            foreach (var d in dic)
            {
                var filed = textInfo.ToTitleCase(d.Key);
                try
                {
                    var value = d.Value;
                    md.GetType().GetProperty(filed).SetValue(md, value);
                }
                catch (Exception e)
                {

                }
            }
            return md;
        }

        /// <summary>
        /// 将字典类型序列化为json字符串
        /// </summary>
        /// <typeparam name="TKey">字典key</typeparam>
        /// <typeparam name="TValue">字典value</typeparam>
        /// <param name="dict">要序列化的字典数据</param>
        /// <returns>json字符串</returns>
        public static string SerializeDictionaryToJsonString<TKey, TValue>(this Dictionary<TKey, TValue> dict)
        {
            if (dict.Count == 0)
                return "";

            string jsonStr = JsonConvert.SerializeObject(dict);
            return jsonStr;
        }

        /// <summary>
        /// 字典封箱到对象,jobject实现
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static object ToObject<TKey, TValue>(this Dictionary<TKey, TValue> dict)
        {
            if (dict != null && dict.Count > 0)
            {
                object o = new object();
                JObject jobject = new();
                dict.ForEach(x => { 
                    if(x.Key!=null && x.Value != null)
                    {
                        JToken jt;
                        //JToken jt = JObject.FromObject(x.Value);
                        try
                        {
                            jt = JToken.FromObject(x.Value);
                        }
                        catch (Exception)
                        {
                            JsonReader read = new JsonTextReader(new StringReader(x.Value.ToString()));
                            jt = JToken.ReadFrom(read);
                        }
                        jobject.Add(x.Key.ToString(), jt);
                        //o.SetPropertyValue(x.Key.ToString(), x.Value);

                    }
                });
                return jobject.ToObject<object>();
            }
            return new object();

            
        }

        public static object? ToExpandoObject(this IDictionary<string, object> dict)
        {
            if (dict != null && dict.Count > 0)
            {
                IDictionary<string, object> expandoObject = new ExpandoObject();
                foreach (var d in dict)
                {
                    expandoObject.Add(d);
                }
                return expandoObject as ExpandoObject;
            }
            return null;
        }
    }
}
